package com.Bride.smarthome.controller.admin.Device;

import com.Bride.common.core.domain.R;
import com.Bride.common.core.web.controller.BaseController;
import com.Bride.common.core.web.page.TableDataInfo;
import com.Bride.smarthome.domain.Device.Device;
import com.Bride.smarthome.service.Device.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理控制器
 *
 * @author Bride
 * @date 2025-09-12
 */
@Tag(name = "管理后台 - 设备管理", description = "设备管理接口")
@RestController
@RequestMapping("/admin/device")
public class DeviceController extends BaseController {

  @Autowired
  private DeviceService deviceService;

  /**
   * 查询设备列表
   */
  @Operation(summary = "查询设备列表")
  @GetMapping("/list")
  public TableDataInfo list(Device device) {
    startPage();
    List<Device> list = deviceService.selectDeviceList(device);
    return getDataTable(list);
  }

  /**
   * 获取设备详细信息
   */
  @Operation(summary = "获取设备详细信息")
  @GetMapping(value = "/{deviceId}")
  public R<Device> getInfo(@PathVariable("deviceId") Long deviceId) {
    return R.ok(deviceService.selectDeviceById(deviceId));
  }

  /**
   * 新增设备
   */
  @Operation(summary = "新增设备")
  @PostMapping
  public R add(@RequestBody Device device) {
    return R.ok(deviceService.insertDevice(device) > 0 ? "添加成功" : "添加失败");
  }

  /**
   * 修改设备
   */
  @Operation(summary = "修改设备")
  @PutMapping
  public R edit(@RequestBody Device device) {
    return R.ok(deviceService.updateDevice(device) > 0 ? "修改成功" : "修改失败");
  }

  /**
   * 删除设备
   */
  @Operation(summary = "删除设备")
  @DeleteMapping("/{deviceIds}")
  public R remove(@PathVariable Long[] deviceIds) {
    int count = 0;
    for (Long deviceId : deviceIds) {
      count += deviceService.deleteDeviceById(deviceId);
    }
    return R.ok(count > 0 ? "删除成功" : "删除失败");
  }
}
